/*
 * Copyright 2017 Baidu, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
package von.seiji.cn.other;

import cn.hutool.core.codec.Base64;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.videoio.VideoCapture;

public class CVUtil {
     static {
         System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
     }
    /*
    normolised euclidian distance
     */
    public static double distance(String v1 , String v2) {
        Mat cv1 = stringToMat(v1);
        Mat cv2 = stringToMat(v2);
        return Core.norm(cv1,cv2,Core.NORM_L2);

    }
    /*
    normolised euclidian distance
     */
    public static double distance(Mat v1 , Mat v2) {
        return Core.norm(v1,v2,Core.NORM_L2);

    }

    /*
    string value of cvmat to cvmat. eg. [0.1,0.2]
     */
    public static Mat stringToMat(String str) {
        String[] vals = str.substring(1,str.length()-2).split(",");
        Mat ret = new Mat(1,vals.length,CvType.CV_64FC1 );
        for (int i =0; i< vals.length ; i++) {
            ret.put(0,i, Double.parseDouble(vals[i]));
        }
        return ret;
    }

    // 从视频流中拿一张照片 base64
    public static String abstractFrame(String url) {
        VideoCapture capture=new VideoCapture();
        boolean ret = capture.open(url);
        System.out.println("opening stream:"+url);
        System.out.println("open stream result:"+ret);

        if(capture.isOpened()){
            Mat frame=new Mat();
            while(true) {
                capture.read(frame);//2 视频文件的视频写入 Mat video 中
                HighGui.imshow("image", frame);//3 显示图像
                int index=HighGui.waitKey(100);//4 获取键盘输入
                if(index==27) {//5 如果是 Esc 则退出
                    capture.release();

                }

                if (!frame.empty()) {
                    break;
                }
            }

            capture.release();

            byte[] data = Mat2Byte(frame,".jpg");
            return Base64.encode(data);
        }
        return null;
    }
    public static byte[] Mat2Byte(Mat matrix, String fileExtension) {
        // convert the matrix into a matrix of bytes appropriate for
        // this file extension
        MatOfByte mob = new MatOfByte();
        Imgcodecs.imencode(fileExtension, matrix, mob);
        // convert the "matrix of bytes" into a byte array
        byte[] byteArray = mob.toArray();

        return byteArray;
    }

}
